package servlet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Singer;
import mapper.SingerMapper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/app")
public class ControllerServlet extends HttpServlet {
    private SingerMapper singerMapper;

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext servletContext = getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        singerMapper = webApplicationContext.getBean("singerMapper", SingerMapper.class);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        if ("singerList".equals(type)) {
            String pagecount = request.getParameter("pagecount");
            if (pagecount == null) {
                pagecount = "1";
            }
            PageHelper.startPage(Integer.parseInt(pagecount), 3);
            List<Singer> allSinger = singerMapper.getAllSinger();
            PageInfo pageInfo = new PageInfo(allSinger);
            int pages = pageInfo.getPages();
            if (Integer.parseInt(pagecount) >= pages) {
                pagecount = String.valueOf(pages);
            }
            PageHelper.startPage(Integer.parseInt(pagecount), 3);
            allSinger = singerMapper.getAllSinger();
            request.setAttribute("pagecount", pagecount);
            request.setAttribute("singerList", allSinger);
            request.getRequestDispatcher("main.jsp").forward(request, response);

        } else if ("singerDel".equals(type)) {
            String id = request.getParameter("id");
            int i = singerMapper.deleteSingerById(id);
            if (i == 1) {
                request.getRequestDispatcher("/app?type=singerList").forward(request, response);
            } else {
                response.getWriter().write("您的网络卡顿，请稍后再试");
            }

        } else if ("singerAdd".equals(type)) {
            String siNo = request.getParameter("SiNo");
            String siName = request.getParameter("SiName");
            String siArea = request.getParameter("SiArea");
            String siSex = request.getParameter("SiSex");
            String siRemark = request.getParameter("SiRemark");
            Singer singer = new Singer(siNo, siName, siArea, siSex, siRemark);
            System.out.print(singer);
            int i = singerMapper.addSinger(singer);
            if (i == 1) {
                request.getRequestDispatcher("/app?type=singerList").forward(request, response);
            } else {
                response.getWriter().write("您的网络卡顿，请稍后再试");
            }

        } else if ("singerDetail".equals(type)) {
            String id = request.getParameter("id");
            Singer singer = singerMapper.getSingerById(id);
            request.setAttribute("singer", singer);
            request.getRequestDispatcher("singerDetail.jsp").forward(request, response);

        } else if ("singerEdit".equals(type)) {
            String id = request.getParameter("id");
            Singer singer = singerMapper.getSingerById(id);
            request.setAttribute("singer", singer);
            request.getRequestDispatcher("singerEdit.jsp").forward(request, response);

        } else if ("singerUpdate".equals(type)) {
            String siNo = request.getParameter("SiNo");
            String siName = request.getParameter("SiName");
            String siArea = request.getParameter("SiArea");
            String siSex = request.getParameter("SiSex");
            String siRemark = request.getParameter("SiRemark");
            Singer singer = new Singer(siNo, siName, siArea, siSex, siRemark);
            int i = singerMapper.updateSinger(singer);
            if (i == 1) {
                request.getRequestDispatcher("/app?type=singerList").forward(request, response);
            } else {
                response.getWriter().write("您的网络卡顿，请稍后再试");
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
